import config from '@/custom/config.js';
import site from '@/custom/siteroot.js';
import api from '@/api';

async function handleSpecialRequest() {
	let adminInfo = uni.getStorageSync('adminInfo');
	return uni.request({
		url: site.siteroot + '/admin/SecondaryPassword/isSetPassword',
		method: 'POST',
		header: {
			Accept: "application/json, text/plain, */*",
			contentType: config.contentType,
			agentId: adminInfo.agent_id,
			batoken: adminInfo.token,
			chainStoresId: adminInfo.chain_stores_id,
			oemId: adminInfo.oem_id,
			partnerId: adminInfo.partner_id,
			server: "true",
			storeId: adminInfo.store_id,
			thinkLang: "zh-cn",
		},
	});
}
async function chouqufangfa(msg) {
	const showInputDialog = () => {
		uni.showModal({
			title: "未设置过二级密码本次输入后自动设置，若设置过本次为验证密码",
			placeholderText: msg,
			content: '',
			editable: true,
			showCancel: true,
			success: (res) => {
				if (res.confirm) {
					if (res.content.trim() === '') {
						uni.showToast({
							icon: 'none',
							title: '请输入二级密码'
						});
						// 重新显示弹窗
						showInputDialog();
					} else {
						handleSpecialRequest().then((specialRes) => {
							if (specialRes.data.code == 1) {
								let adminInfo = uni.getStorageSync('adminInfo');
								uni.request({
									url: site.siteroot +
										'/admin/SecondaryPassword/checkPassword',
									method: 'post',
									data: {
										old_password: '',
										password: res.content
									},
									header: {
										Accept: "application/json, text/plain, */*",
										contentType: config.contentType,
										agentId: adminInfo.agent_id,
										batoken: adminInfo.token,
										chainStoresId: adminInfo.chain_stores_id,
										oemId: adminInfo.oem_id,
										partnerId: adminInfo.partner_id,
										server: "true",
										storeId: adminInfo.store_id,
										thinkLang: "zh-cn",
									},
								}).then(res => {
									if (res.data.code === 1) {
										uni.setStorageSync("Secondary_Token", res
											.data
											.data.token);
										setTimeout(() => {
											uni.showToast({
												icon: 'none',
												title: res.data.msg,
												duration: 2000
											});
										}, 500);
									} else {
										setTimeout(() => {
											uni.showToast({
												icon: 'none',
												title: res.data.msg,
												duration: 2000
											});
										}, 500);
										setTimeout(res => {
											showInputDialog();
										}, 1500)
									}
									uni.hideLoading()
								});
							} else {
								let adminInfo = uni.getStorageSync('adminInfo');
								uni.request({
									url: site.siteroot +
										'/admin/SecondaryPassword/setPassword',
									method: 'post',
									data: {
										old_password: '',
										password: res.content
									},
									header: {
										Accept: "application/json, text/plain, */*",
										contentType: config.contentType,
										agentId: adminInfo.agent_id,
										batoken: adminInfo.token,
										chainStoresId: adminInfo.chain_stores_id,
										oemId: adminInfo.oem_id,
										partnerId: adminInfo.partner_id,
										server: "true",
										storeId: adminInfo.store_id,
										thinkLang: "zh-cn",
									},
								}).then(res => {
									if (res.data.code === 1) {
										let adminInfo = uni.getStorageSync(
											'adminInfo');
										uni.request({
											url: site.siteroot +
												'/admin/SecondaryPassword/checkPassword',
											method: 'post',
											data: {
												old_password: '',
												password: res.content
											},
											header: {
												Accept: "application/json, text/plain, */*",
												contentType: config
													.contentType,
												agentId: adminInfo.agent_id,
												batoken: adminInfo.token,
												chainStoresId: adminInfo
													.chain_stores_id,
												oemId: adminInfo.oem_id,
												partnerId: adminInfo
													.partner_id,
												server: "true",
												storeId: adminInfo.store_id,
												thinkLang: "zh-cn",
											},
										}).then(res => {
											if (res.data.code === 1) {
												uni.setStorageSync(
													"Secondary_Token",
													res.data
													.data.token);
												setTimeout(() => {
													uni.showToast({
														icon: 'none',
														title: res
															.data
															.msg,
														duration: 2000
													});
												}, 500);
											} else {
												setTimeout(() => {
													uni.showToast({
														icon: 'none',
														title: res
															.data
															.msg,
														duration: 2000
													});
												}, 500);
												setTimeout(res => {
													showInputDialog
														();
												}, 1500)
											}
										});
									} else {
										setTimeout(() => {
											uni.showToast({
												icon: 'none',
												title: res.data.msg,
												duration: 2000
											});
										}, 500);
										setTimeout(res => {
											showInputDialog();
										}, 1500)
									}
								});
							}
						}).catch((specialErr) => {
							let adminInfo = uni.getStorageSync('adminInfo');
							uni.request({
								url: site.siteroot +
									'/admin/SecondaryPassword/setPassword',
								method: 'post',
								data: {
									old_password: '',
									password: res.content
								},
								header: {
									Accept: "application/json, text/plain, */*",
									contentType: config.contentType,
									agentId: adminInfo.agent_id,
									batoken: adminInfo.token,
									chainStoresId: adminInfo.chain_stores_id,
									oemId: adminInfo.oem_id,
									partnerId: adminInfo.partner_id,
									server: "true",
									storeId: adminInfo.store_id,
									thinkLang: "zh-cn",
								},
							}).then(res => {
								if (res.code === 1) {
									let adminInfo = uni.getStorageSync('adminInfo');
									uni.request({
										url: site.siteroot +
											'/admin/SecondaryPassword/checkPassword',
										method: 'post',
										data: {
											old_password: '',
											password: res.content
										},
										header: {
											Accept: "application/json, text/plain, */*",
											contentType: config.contentType,
											agentId: adminInfo.agent_id,
											batoken: adminInfo.token,
											chainStoresId: adminInfo
												.chain_stores_id,
											oemId: adminInfo.oem_id,
											partnerId: adminInfo.partner_id,
											server: "true",
											storeId: adminInfo.store_id,
											thinkLang: "zh-cn",
										},
									}).then(res => {
										if (res.code === 1) {
											uni.setStorageSync(
												"Secondary_Token", res
												.data
												.data.token);
											setTimeout(() => {
												uni.showToast({
													icon: 'none',
													title: res
														.data
														.msg,
													duration: 2000
												});
											}, 500);
										} else {
											setTimeout(() => {
												uni.showToast({
													icon: 'none',
													title: res
														.data
														.msg,
													duration: 2000
												});
											}, 500);
											setTimeout(res => {
												showInputDialog();
											}, 1500)
										}
									});
								}
							});
						});
					}
				} else {
					uni.navigateBack({
						delta: 1
					});
				}
			}
		});
	};

	showInputDialog(); // 显示弹窗
}

export default {
	request: async function(option) {
		if (option.mask) {
			uni.showLoading({
				title: option.mask == 1 ? '加载中' : option.mask,
				mask: true
			});
		}
		var option = option || {};
		if (!option.url) {
			return false;
		}
		return new Promise((resolve, reject) => {
			let adminInfo = uni.getStorageSync('adminInfo');
			let Secondary_Token = uni.getStorageSync('Secondary_Token')
			if (option.url != '/admin/Index/login') {
				if (!adminInfo.token) {
					uni.reLaunch({
						url: '/pages/login/index'
					})
					return;
				}
			}
			uni.request({
				url: site.siteroot + option.url,
				data: option.data ? option.data : {},
				method: option.method ? option.method : 'GET',
				header: {
					Accept: "application/json, text/plain, */*",
					contentType: config.contentType,
					agentId: adminInfo.agent_id,
					batoken: adminInfo.token,
					chainStoresId: adminInfo.chain_stores_id,
					oemId: adminInfo.oem_id,
					partnerId: adminInfo.partner_id,
					server: "true",
					storeId: adminInfo.store_id,
					thinkLang: "zh-cn",
					'Kd-Secondary-Token': Secondary_Token
				},
				complete: (res) => {
					if (option.mask) {
						uni.hideLoading();
					}
					if (res?.data?.code == 1) {
						resolve(res.data)
					} else {
						if (res?.data?.code == 400) {
							resolve(res.data)
							config.tokenErrorMessage(res.data.msg || res.msg)
						} else if (res?.data?.code == 303 || res?.data?.code == 409) {
							config.tokenErrorMessage(res.data.msg || res.msg)
							uni.removeStorageSync('token')
							uni.removeStorageSync('storeId')
							uni.reLaunch({
								url: '/pages/login/index'
							})
						} else if (res?.data?.code == 202) {
							handleSpecialRequest().then((specialRes) => {
								if (specialRes.data.code == 1) {
									chouqufangfa(res.data.msg);
								} else {
									chouqufangfa(res.data.msg);
								}
							}).catch((specialErr) => {
								chouqufangfa(res.data.msg);
							});
						} else {
							config.tokenErrorMessage(res.data.msg || res.msg)
						}
					}
				}
			});
		})
	}
}